package com.choosefine.paycenter.webgateway.account.web.controller;

import com.choosefine.paycenter.account.dto.AccountBankcardDto;
import com.choosefine.paycenter.account.dto.AccountBankcardSetUBankCodeAndNameDto;
import com.choosefine.paycenter.account.dto.AccountCompanyBankcardVerifyDto;
import com.choosefine.paycenter.account.enums.BankcardTypeEnum;
import com.choosefine.paycenter.account.model.AccountBankcard;
import com.choosefine.paycenter.account.service.AccountBankcardService;
import com.choosefine.paycenter.account.service.AccountService;
import com.choosefine.paycenter.common.dto.JSONResult;
import com.choosefine.paycenter.common.utils.JSONResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * Comments：
 * Author：DengYouyi
 * Create Date：2017/3/9
 * Modified By：
 * Modified Date：
 * Why & What is modified：
 * Version：v1.0
 */
@RestController
@RequestMapping("bankCard")
@Api(value = "银行卡", description = "邓友谊")
public class AccountBankcardController {
    @Autowired
    private AccountBankcardService accountBankcardService;

    @Autowired
    private AccountService accountService;

    /**
     * 添加银行卡
     *
     * @return
     */
    @PostMapping("companyBankCard")
    @ApiOperation("添加企业银行卡")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode", paramType = "header", dataType = "String", required = true, value = "租户CODE"),
            @ApiImplicitParam(name = "bankAccountName", paramType = "body", dataType = "String", required = true, value = "银行账户名称"),
            @ApiImplicitParam(name = "bankcardNo", paramType = "body", dataType = "String", required = true, value = "银行卡号"),
            @ApiImplicitParam(name = "bankUnionCode", paramType = "body", dataType = "String", required = true, value = "银行支行联行号"),
            @ApiImplicitParam(name = "bankUnionName", paramType = "body", dataType = "String", required = true, value = "银行卡支行名"),
            @ApiImplicitParam(name = "bankCode", paramType = "body", dataType = "String", required = true, value = "银行code"),
            @ApiImplicitParam(name = "bankName", paramType = "body", dataType = "String", required = true, value = "银行名称")
    })
    public JSONResult companyBankCard(@RequestHeader(value = "userCode") @NotBlank String userCode, @Validated({AccountBankcardService.AddCompanyAccountBankCard.class}) @RequestBody AccountBankcardDto accountBankcardDto) {
        long id = accountBankcardService.addCompanyBankCard(userCode, accountBankcardDto);
        return JSONResultGenerator.buildCreatedSuccessResult(id);
    }


    /**
     * 添加银行卡
     *
     * @return
     */
    @PutMapping("companyBankCard/verify")
    @ApiOperation("验证打款金额")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode", paramType = "header", dataType = "String", required = true, value = "租户CODE"),
            @ApiImplicitParam(name = "accountBankcardId", paramType = "body", dataType = "Long", required = true, value = "账户银行卡id"),
            @ApiImplicitParam(name = "validAmount", paramType = "body", dataType = "String", required = true, value = "验证金额"),
    })
    public JSONResult verifyCompanyBankCard(@RequestHeader(value = "userCode") @NotBlank String userCode, @RequestBody AccountCompanyBankcardVerifyDto accountCompanyBankcardVerifyDto) {
        accountBankcardService.verifyCompanyBankcard(userCode, accountCompanyBankcardVerifyDto);
        return JSONResultGenerator.buildOperSuccessResult();
    }


    @PostMapping("personalBankCard")
    @ApiOperation("添加个人银行卡")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode", paramType = "header", dataType = "String", required = true, value = "租户CODE"),
            @ApiImplicitParam(name = "bankAccountName", paramType = "body", dataType = "String", required = true, value = "银行账户名称"),
            @ApiImplicitParam(name = "bankcardNo", paramType = "body", dataType = "String", required = true, value = "银行卡号")
    })
    public JSONResult addPersonalBankCard(@RequestHeader(value = "userCode") @NotBlank String userCode, @Validated({AccountBankcardService.AddPeronalAccountBankCard.class}) @RequestBody AccountBankcardDto accountBankcardDto) {
        long id = accountBankcardService.addPersonalBankCard(userCode, accountBankcardDto);
        return JSONResultGenerator.buildCreatedSuccessResult(id);
    }


    /**
     * 删除银行卡
     *
     * @return
     */
    @DeleteMapping("delBankCard")
    @ApiOperation("删除银行卡")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode", paramType = "header", dataType = "String", required = true, value = "租户CODE"),
            @ApiImplicitParam(name = "accountBankCardId", paramType = "query", dataType = "Long", required = true, value = "银行卡ID")
    })
    public JSONResult delBankCard(@RequestHeader(value = "userCode") @NotBlank String userCode, @NotNull @RequestParam("accountBankCardId") Long accountBankCardId) {
        Long accountId = accountService.findAccountIdByUserCode(userCode);
        int res = accountBankcardService.deleteAccountBankCard(accountId, accountBankCardId);
        return JSONResultGenerator.buildOperSuccessResult(res);
    }

    /**
     * 银行卡验证通过
     *
     * @return
     */
    @PutMapping("validBankCard")
    @ApiOperation("银行卡验证通过")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode", paramType = "header", dataType = "String", required = true, value = "租户CODE"),
            @ApiImplicitParam(name = "accountBankCardId", paramType = "query", dataType = "Long", required = true, value = "银行卡ID")
    })
    public JSONResult validBankCard(@RequestHeader(value = "userCode") @NotBlank String userCode, @NotBlank @RequestParam("accountBankCardId") Long accountBankCardId) {
        Long accountId = accountService.findAccountIdByUserCode(userCode);
        int res = accountBankcardService.validAccountBankCard(accountId, accountBankCardId);
        return JSONResultGenerator.buildOperSuccessResult(res);
    }


    /**
     * 获取银行卡列表
     *
     * @return
     */
    @ApiOperation("获取银行卡列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode", paramType = "header", dataType = "String", required = true, value = "租户CODE"),
            @ApiImplicitParam(name = "idisPublic", paramType = "query", dataType = "Boolean", required = false, value = "是否是企业银行卡")
    })
    @RequestMapping(value = "/bankCardList", method = RequestMethod.GET)
    public JSONResult bankCardList(@RequestHeader(value = "userCode") @NotBlank String userCode, Boolean idisPublic) {
        Long accountId = accountService.findAccountIdByUserCode(userCode);
        Integer isPublic = null;
        if (idisPublic != null) {
            if (idisPublic) {
                isPublic = BankcardTypeEnum.PUBLIC.getId();
            } else if (!idisPublic) {
                isPublic = BankcardTypeEnum.PERSONEL.getId();
            }
        }
        List<AccountBankcard> res = accountBankcardService.getBankCardList(accountId, isPublic);
        return JSONResultGenerator.buildQuerySuccessResult(res);
    }

    /**
     * 获取可被使用的银行卡列表
     *
     * @return
     */
    @ApiOperation("获取可被使用的银行卡列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode", paramType = "header", dataType = "String", required = true, value = "租户CODE"),
    })
    @RequestMapping(value = "/usedBankCardList", method = RequestMethod.GET)
    public JSONResult usedBankCardList(@RequestHeader(value = "userCode") @NotBlank String userCode) {
        Long accountId = accountService.findAccountIdByUserCode(userCode);
        List<AccountBankcard> res = accountBankcardService.getUsedBankCardList(accountId);
        return JSONResultGenerator.buildQuerySuccessResult(res);
    }

    @ApiOperation("设置联行号及支行名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode", paramType = "header", dataType = "String", required = true, value = "租户CODE"),
            @ApiImplicitParam(name = "accountBankcardId", paramType = "body", dataType = "Long", required = true, value = "账户银行卡id"),
            @ApiImplicitParam(name = "bankUnionCode", paramType = "body", dataType = "String", required = true, value = "联行号"),
            @ApiImplicitParam(name = "bankUnionName", paramType = "body", dataType = "String", required = true, value = "支行名称"),
    })
    @RequestMapping(value = "/setUBankCodeAndUbankName", method = RequestMethod.PUT)
    public JSONResult setUnionBankCodeAndName(@RequestHeader("userCode") String userCode, @RequestBody AccountBankcardSetUBankCodeAndNameDto accountBankcardSetUBankCodeAndNameDto) {
        accountBankcardSetUBankCodeAndNameDto.setUserCode(userCode);
        accountBankcardService.setUnionBankCodeAndName(accountBankcardSetUBankCodeAndNameDto);
        return JSONResultGenerator.buildOperSuccessResult();
    }

    @ApiOperation("获取某个资金账号的某张银行卡信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode", paramType = "header", dataType = "String", required = true, value = "租户CODE"),
            @ApiImplicitParam(name = "accountBankcardId", paramType = "query", dataType = "Long", required = true, value = "账户银行卡id"),
    })
    @RequestMapping(value = "/findAccountBankcardById", method = RequestMethod.GET)
    public JSONResult findAccountBankcardById(@RequestHeader("userCode") String userCode,@RequestParam("accountBankcardId") Long accountBankcardId){
        AccountBankcard accountBankcard = accountBankcardService.findAccountBankById(accountBankcardId);
        final String bankcardNo = accountBankcard.getBankcardNo();
        final int lenOfBankcardNo = bankcardNo.length();
        accountBankcard.setBankcardNo(bankcardNo.substring(lenOfBankcardNo-4,lenOfBankcardNo));
        return JSONResultGenerator.buildQuerySuccessResult(accountBankcard);
    }

    @ApiOperation("卡bin校验银行卡号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode", paramType = "header", dataType = "String", required = true, value = "租户CODE"),
            @ApiImplicitParam(name = "bankcardNo", paramType = "query", dataType = "String", required = true, value = "银行卡号"),
    })
    @RequestMapping(value = "/verifyBankcardNo", method = RequestMethod.GET)
    public JSONResult findAccountBankcardById(@RequestHeader("userCode") String userCode,@Validated @RequestParam("bankcardNo") @NotBlank @Length(min = 14,message = "银行卡卡号至少14位") String bankcardNo){
        accountBankcardService.verifyBankcardNo(bankcardNo);
        return JSONResultGenerator.buildOperSuccessResult();
    }

}
